<script type="text/javascript">
$(document).ready(function(){
    $('#price').priceFormat({
        prefix: 'VNĐ ',
        centsSeparator: '',
        centsLimit:0,
        thousandsSeparator: '.'
    });
});
</script>
<script>
$('#end_time').ready(function(){
    $('#end_time').datepicker({
        altFormat: "yy-mm-dd",
        changeYear: true,
        changeMonth: true,
        dateFormat: "yy-mm-dd h:i:s",
        yearRange: "2011:2020"
    });
});
</script>
<script type="text/javascript" >
	$(function(){
		var btnUpload = $('#upload');
		var status = $('#status');
		new AjaxUpload(btnUpload, {
			action: 'upload_file.php',
			name: 'uploadfile',
			onSubmit: function(file, ext){
				 if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){ 
                    // extension is not allowed 
					status.text('Only JPG, PNG or GIF files are allowed');
					return false;
				}
				status.text('Uploading...');
			},
			onComplete: function(file, response){
				//On completion clear the status
				status.text('');
				//Add uploaded file to list
				if(response === "success"){
				    $('#files').html('<img src="images/'+file+'" alt="" width="100" height="100" /><br />'+file).addClass('success');
				} else{
					$('#files').text(file).addClass('error');
				}
			}
		});		
	});
</script>
<?php 
if(isset($_POST['btnSubmit']))
{
    $uploaddir = 'images/';
    $file = $uploaddir . basename($_FILES['uploadfile']['name']);
    $product_name = addslashes($_POST['productname']);
    $category_id = addslashes($_POST['category_id']);
    $description = addslashes($_POST['description']);
    $fields_array = array(
        "product_name"  => "'$product_name'",
        "category_id"   => "'$category_id'",
        "description"   => "'$password'",
        "image"         => "'$file'",
        "create_date"     => "now()"
    );
    $result = insert("mid_products", $fields_array);
    if ($result)
        print "Sản phẩm {$product_name} đã được tạo. <a href='#'>Nhấp vào đây để xem</a>";
    else
        print "Có lỗi trong quá trình đăng bán, Vui lòng liên hệ BQT";
}
else
{
?>
<form action="" enctype="multipart/form-data" method="post" class="grid_24 reg">
<table>
    <tr>
        <td>Tên sản phẩm: </td>
	   <td><input type="text" name="productname" class="textInputreg" value=""/></td>
    </tr>
    <tr>
        <td>Thuộc danh mục: </td>
	   <td><select name="category_id">
        <?php
        $result = getCategoryList();
        foreach($result as $row)
        {
            echo '<option value="'.$row['id'].'">'. $row['category_name'] .'</option>';
        }
        ?> 
       </select></td>
    </tr>
    <tr>
        <td>Mô tả</td>
	   <td><textarea name="description" id="description" rows="5" cols="30"></textarea></td>
    </tr>
    <script type="text/javascript" language="javascript">
    CKEDITOR.replace('description');
    </script>
    <tr>
        <td>Giá bán</td>
	   <td><input type="text" name="price" id="price" class="textInputreg" value=""/></td>
    </tr>
    <tr>
        <td>Thời gian kết thúc</td>
	   <td><input type="text" name="end_time" id="end_time" class="textInputreg" value=""/></td>
    </tr>
    <tr>
        <td>Ảnh đại diện</td>
	    <td>
            <input type="file" name="uploadfile" value="" id="upload" /><span id="status" ></span><ul id="files"></ul>
    	</td>
    </tr>
</table>
<input type="submit" class="button" name="btnSubmit" value="Đăng bán" />
<input type="reset" class="button" name="reset" id="reset" value="Làm lại" />
</form>
<?php
}
?>